package com.wuyou.simple;

/**
 * 374. 猜数字大小
 */
public class GuessNumber {


    public static void main(String[] args) {
        int n = 10, p = 6;
        System.out.println(sol_1(n, p));
    }


    public static int sol_1(int n, int p){
        int pick = 0, l = 0, r = n, mid = n / 2;

        while(l <= r){
            int res = p == mid ? 0 : (p > mid) ? 1 : -1;
            if(res == 0){
                pick = mid;
            }else if(res == -1){
                r = mid - 1;
                mid = ((r - l) / 2) + l;
            }else if(res == 1){
                l = mid + 1;
                mid = ((r - l) / 2) + l;
            }else{
                l ++;
                r --;
            }
        }
        return pick;
    }
}
